package com.hzt.agprosaleplatform.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hzt.agprosaleplatform.common.CustomResponse;
import com.hzt.agprosaleplatform.entity.Good;
import com.hzt.agprosaleplatform.entity.Shop;
import com.hzt.agprosaleplatform.mapper.ShopMapper;
import com.hzt.agprosaleplatform.service.ShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ShopServiceImpl implements ShopService {

    @Autowired
    ShopMapper shopMapper;

    @Override
    public CustomResponse CreateShop(Shop shop) {
        try {
            // 使用 MyBatis-Plus 的 QueryWrapper 来查询
            QueryWrapper<Shop> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("uid", shop.getUid());
            Shop existShop = shopMapper.selectOne(queryWrapper);

            if(existShop != null){
                return new CustomResponse(400, "已存在商铺，无法再创建新的商铺", null);
            }
            
            // 创建新商铺
            int result = shopMapper.insert(shop);
            return result > 0 
                ? new CustomResponse(200, "创建商铺成功", null)
                : new CustomResponse(500, "创建商铺失败", null);
        } catch (Exception e) {
            e.printStackTrace();
            return new CustomResponse(500, "创建商铺失败：" + e.getMessage(), null);
        }
    }

    @Override
    public CustomResponse UpdateShopInfo(Shop shop) {
        try{
            Shop existShop = shopMapper.selectById(shop.getShopId());
            if(existShop == null){
                return new CustomResponse(500,"商铺不存在",null);
            }

            if(!existShop.getUid().equals(shop.getUid())){
                return new CustomResponse(500,"无权修改店铺信息",null);
            }

            int result = shopMapper.updateById(shop);
            return result > 0
                    ? new CustomResponse(200, "商铺信息更新成功", null)
                    : new CustomResponse(500, "商铺信息更新失败", null);

        }catch (Exception e){
            return new CustomResponse(500,"更新店铺信息失败",null);
        }
    }

    //根据uid返回商铺
    @Override
    public Shop selectShopByUId(int uid) {
        // 创建查询条件
        QueryWrapper<Shop> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uid", uid);
        return shopMapper.selectOne(queryWrapper);
    }

    //根据shopId返回商铺
    @Override
    public Shop selectShopByShopID(int shopID) {
        return shopMapper.selectById(shopID);
    }

    //查询Shop_id下发布的商品
   /* @Override
    public List<Good> selectShopGoodsByShopId(Integer shopID) {
        return shopMapper.selectGoodsByShopId(shopID);
    }*/
    /**
     * 原有：根据商铺ID查询商品
     */
    @Override
    public List<Good> selectShopGoodsByShopId(Integer shopID) {
        // 验证商铺是否存在（避免查询不存在的商铺）
        Shop shop = shopMapper.selectById(shopID);
        if (shop == null) {
            return new ArrayList<>();
        }
        // 调用Mapper查询商品
        return shopMapper.selectGoodsByShopId(shopID);
    }

// 新增：实现查询所有商铺（无筛选条件，返回全部商铺）
@Override
    public List<Shop> getAllShops() {
        // QueryWrapper(null) 表示无查询条件，查询表中所有数据
        return shopMapper.selectList(null);
    }


}
